<template>
  <header class="header">
    <h1>todos</h1>
    <input id="toggle-all" class="toggle-all" type="checkbox" v-model="isAll" />
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model.trim="name"
      @keyup.enter="addHeader"
    />
  </header>
</template>

<script>
import { mapMutations, mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {
      name: "",
    };
  },

  methods: {
    ...mapActions(["allSel"]),
    ...mapMutations(["addList", "chooseAll", "allSel"]),
    addHeader() {
      if (this.name.length === 0) return alert("请输入内容!");
      this.addList(this.name);
      this.name = "";
    },
  },
  computed: {
    ...mapGetters(["showList"]),
    isAll: {
      get() {
        if (this.showList.length === 0) return false;
        return this.showList.every((item) => {
          return item.isDone;
        });
      },
      set(val) {
        this.chooseAll(val);
        // return this.showList.forEach((item) => {
        //   item.isDone = val;
        // });
      },
    },
  },
};
</script>
